package com.wxl.service;

import com.wxl.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class SnowflakeService {

    @Autowired
    private SnowflakeIdWorker idWorker;

    public long generateIdNoConCurr() {

        // 并发小时 一个毫秒只有1个id 都是偶数
        Random random = new Random();
        int r = random.nextInt(20);
        long orderId = idWorker.nextId();
        for(int i=0; i<=r ; i++){
            orderId = idWorker.nextId();
        }

        return orderId;
    }

    public long generateId() {
        // 高并发  一个毫秒可以有多个id
        return idWorker.nextId();
    }

}

